using System;
using System.Globalization;
using System.Windows.Data;

namespace Phone.Framework.Converters
{
    public class DeductOneDayConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value is DateTime)
            {
                var newValue = ((DateTime) value).AddDays(-1);
                if (parameter != null)
                    return string.Format(parameter.ToString(), newValue);
                return newValue;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                return ((DateTime)value).AddDays(1);
            }
            return value;
        }     
    }

    public class RedeemDaysConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                var newValue = ((DateTime)value).AddDays(System.Convert.ToInt32(parameter));
                //if (parameter != null)
                //    return string.Format(parameter.ToString(), newValue);
                return newValue;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                return ((DateTime)value).AddDays(-System.Convert.ToInt32(parameter));
            }
            return value;
        }
    }

    public class RedeemHourConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                var newValue = ((DateTime)value).AddHours(System.Convert.ToInt32(parameter));
              
                return newValue;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                return ((DateTime)value).AddHours(-System.Convert.ToInt32(parameter));
            }
            return value;
        }
    }
    
}